﻿/*
 * Created by SharpDevelop.
 * User: user
 * Date: 20.11.2012
 * Time: 9:29
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WinFormsTest
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		
		[DllImport("shell32.dll",EntryPoint = "ExtractIcon")]
        extern static IntPtr ExtractIcon(IntPtr hInst,string lpszExeFileName,int nIconIndex);

        
		
        
        string _dllpath=String.Empty;
        List<objectIcon> lstIcons= new List<objectIcon>();
        
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		void DataGridView1CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
			
		}
		
		void Button1Click(object sender, EventArgs e)
		{
			using(OpenFileDialog dlg= new OpenFileDialog())
			{
				if(DialogResult.OK==dlg.ShowDialog())
				{
					_dllpath=dlg.FileName;
				}
			}
			
			ProcessIcons();
		}
		
		void ProcessIcons()
		{
			for(int i=1;i<500;i++)
			{
				try {
					objectIcon obj= new objectIcon();
					obj.Id=i;
					IntPtr pHandle=ExtractIcon(IntPtr.Zero,_dllpath,i);
					obj.Icon=Icon.FromHandle(pHandle).ToBitmap();
					
					lstIcons.Add(obj);
				} catch (Exception) {
					
					continue;
				}
			}
			
			foreach (objectIcon obj in lstIcons) {
				int index=dataGridView1.Rows.Add();
				dataGridView1.Rows[index].Cells["Id"].Value=obj.Id;
				dataGridView1.Rows[index].Cells["Image"].Value=obj.Icon;
			}
		}
		
		void DataGridView1RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
		{
			
		}
	}
}
